/**
 * 版权所有 2009-2015 旺晓杨 peonyxy@msn.cn
 **/
package com.cn;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.ac.cintcm.sh.domain.Article;
import cn.ac.cintcm.sh.domain.Prescription;
import cn.ac.cintcm.sh.service.HerbService;
import cn.ac.cintcm.sh.service.PrescriptionService;
import cn.ac.cintcm.sh.service.TextService;

public class HerbServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public HerbServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setCharacterEncoding("UTF-8");
		String doClass = request.getParameter("doClass");
		if(doClass != null && doClass.equals("herPr"))
		{
			this.herPr(request, response);
		}
		else if(doClass != null && doClass.equals("herbTextArea"))
		{
			this.herbTextArea(request, response);
		}
		else if(doClass != null && doClass.equals("herSimilar"))
		{
			this.herSimilar(request, response);
		}
		else if(doClass != null && doClass.equals("herbMostPr"))
		{
			this.herbMostPr(request, response);
		}
		
	}
	
	/****
	 * 多中药组成方剂
	 * @param request
	 * @param response
	 */
	public void herbMostPr(HttpServletRequest request, HttpServletResponse response){
		String[] herIds = request.getParameterValues("herIds");
		if(herIds == null || herIds.length == 0)
			return;
		 PrintWriter out = null;
		 StringBuffer str = new StringBuffer("");
			try {
				PrescriptionService prescriptionService = new PrescriptionService();

				Iterator iterator = prescriptionService.getPrescriptions(herIds).iterator();
				str.append("{articles:[");
				if(iterator.hasNext())
				{
					Prescription prescription = (Prescription)iterator.next();
					
					str.append("{'id':"+prescription.getId()+",'name':'"+prescription.getName()+"','herb':'"+prescription.getText()+"'}");
			
					while(iterator.hasNext()){
						 prescription = (Prescription)iterator.next();
						str.append(",{'id':"+prescription.getId()+",'name':'"+prescription.getName()+"','herb':'"+prescription.getText()+"'}");
					}
				}
				str.append("]}");
				 out = response.getWriter();
			} catch (Exception e) {
				str.append("{articles:[{'id':'','text':'<font color=red>������Ŵ���!</font>'}]}");
			}
		 out.print(str);
		 out.flush();
	}
	
	/**
	 * �����Ʋ�获得相似中药����
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	public void herSimilar(HttpServletRequest request, HttpServletResponse response){
		String herId = request.getParameter("herId");
		 PrintWriter out = null;
		 StringBuffer str = new StringBuffer("");
			
			try {
				HerbService herbService = new HerbService();

				Iterator iterator = herbService.getPrescriptions(herId).iterator();
				str.append("{articles:[");
				str.append("{'id':'1','qi':'<font color=red>尚未提供接口!</font>'}");
				str.append("]}");
				 out = response.getWriter();
			} catch (Exception e) {
				str.append("{articles:[{'id':'','text':'<font color=red>查询错误!</font>'}]}");
			}
		 out.print(str);
		 out.flush();
	}
	
	
	/**
	 * �����Ʋ�获取中药所在的所有方剂����
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	public void herPr(HttpServletRequest request, HttpServletResponse response){
		String herId = request.getParameter("herId");
		 PrintWriter out = null;
		 StringBuffer str = new StringBuffer("");
			
			try {
				HerbService herbService = new HerbService();

				Iterator iterator = herbService.getPrescriptions(herId).iterator();
				str.append("{articles:[");
				if(iterator.hasNext())
				{
					Prescription prescription = (Prescription)iterator.next();
					str.append("{'id':'"+prescription.getId()+"','name':'"+prescription.getName()+"'}");
			
					while(iterator.hasNext()){
						 prescription = (Prescription)iterator.next();
						str.append(",{'id':'"+prescription.getId()+"','name':'"+prescription.getName()+"'}");
					}
				}
				str.append("]}");
				 out = response.getWriter();
			
			} catch (Exception e) {
				str.append("{articles:[{'id':'','text':'<font color=red>查询错误!</font>'}]}");
			}
		 out.print(str);
		 out.flush();
	}
	
	/**
	 * 获取中药相关原文
	 * 	 * @param request
	 * @param response
	 * @throws IOException
	 */
	public void herbTextArea(HttpServletRequest request, HttpServletResponse response){
		String herId = request.getParameter("herId");
		 PrintWriter out = null;
		 StringBuffer str = new StringBuffer("");
			
			try {
				TextService textService = new TextService();

				Iterator iterator = textService.getHerbArticles(herId).iterator();
				str.append("{articles:[");
				if(iterator.hasNext())
				{
					Article prescription = (Article)iterator.next();
					str.append("{'id':'"+prescription.getId()+"','text':'"+prescription.getText()+"'}");
			
					while(iterator.hasNext()){
						 prescription = (Article)iterator.next();
						str.append(",{'id':'"+prescription.getId()+"','text':'"+prescription.getText()+"'}");
					}
				}
				str.append("]}");
				 out = response.getWriter();
			} catch (Exception e) {
				str.append("{articles:[{'id':'','text':'<font color=red>查询错误!</font>'}]}");
			}
		 out.print(str);
		 out.flush();
	}
	
	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}